<?php

namespace seecms\controller;

use Overtrue\Pinyin\Pinyin;
use seecms\db\SeeDbException;
use seecms\develop\DevelopModule;
use seecms\lib\response\JsonResponse;
use seecms\region\lib\Region;
use seecms\See;
use seecms\lib\Auth;
use seecms\SeeLang;

class Home extends SeeBaseController
{
    /**
     * @return string
     */
    public function index(): string
    {
        $right_toolbar = See::config()->right_toolbar->config;
        $userinfo = Auth::user()->info();
        $lang = See::config()->lang->config;

        return See::view()->fetch('layout/index', compact('right_toolbar', 'userinfo', 'lang'));
    }

    /**
     * 后台首页
     * @return string
     */
    public function main(): string
    {
        $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];

        return $this->view('home/main',
            [
                'phpversion' => phpversion(),
                'sys_info' => $sys_info,
                'tips' => See::config()->debug(),
            ]
        );
    }

    public function menu(): array
    {
        return Auth::user()->menu();
    }

    /**
     * @return array
     * @throws SeeDbException
     */
    public function ruleApply(): array
    {
        $title = See::request()->post('title');
        $url = See::request()->post('url');
        $remark = See::request()->post('remark');

        // 验证用户是否已经拥有权限
        $rules = Auth::user()->rule();
        if (in_array($url, $rules)) {
            return $this->fail(SeeLang::rule('verifying'), 1, ['status' => 1]);
        }

        $record = See::table()->user_rule_apply->where('url', $url)
            ->where('user_id', Auth::user()->uuid())
            ->where('status', 'in', [0, 1])
            ->order('id desc')
            ->find();
        if ($record) {
            return $this->fail(SeeLang::rule('verifying'), 1, $record);
        }

        $data = [
            'user_id' => Auth::user()->uuid(),
            'title' => $title,
            'url' => $url,
            'remark' => $remark,
        ];

        See::table()->user_rule_apply->insert($data);

        return $this->success(null, SeeLang::rule('verify_success'));
    }

    /**
     * 清除缓存
     * @return array
     */
    public function clearCache(): array
    {
        $type = See::request()->param('type');
        // 清除菜单、权限缓存
        Auth::user()->clearCachePermission($type);

        // 重新生成语言包文件
        make_lang_json(See::lang()->getLangSet());

        return $this->success();
    }

    public function switchLangSet()
    {
        $lang = See::request()->param('lang');

        // 清除菜单
        Auth::user()->clearCachePermission('menu');
        // 创建前端语言包文件
        make_lang_json($lang);

        return $this->success();
    }

    public function upload(): array
    {
        try {
            $upload = See::provider()->upload;
            $filepath = $upload->move();
        } catch (\RuntimeException $exception) {
            return $this->fail($exception->getMessage());
        }

        return $this->success(['url' => $filepath], SeeLang::upload('success'));
    }

    /**
     * 附件管理
     * @return string|JsonResponse
     * @throws SeeDbException
     */
    public function choose()
    {
        $limit = See::request()->get('limit');
        $ext = See::request()->get('ext', 'png|jpg');
        $extension = explode('|', $ext);

        $json = See::request()->get('json');

        if ($json) {
            $records = See::table()->attachment->where('ext', 'in', $extension)->paginate();
            return JsonResponse::success($records);
        }

        $total = See::table()->attachment->where('ext', 'in', $extension)->count();

        return $this->view('attachment/choose', [
            'total' => $total,
            'ext' => $ext,
            'limit' => $limit
        ]);
    }
}